import { useKeyPress, useEventListener } from "ahooks";
import { useMount } from "./use";
import { KEYS } from "./types";

let keys = [
  "Control",
  "Alt",
  "Shift",
  "ArrowUp",
  "ArrowDown",
  "ArrowLeft",
  "ArrowRight",
  "Enter"
];

export const useKeyboard = () => app => {
  useMount(() => {
    app.ctx.keyPress = 0b00000000;
  });
  useKeyPress(
    event => keys.includes(event.key),
    event => {
      let { key } = event;
      if (event.type === "keyup") {
        app.ctx.keyPress &= ~KEYS[key];
      } else if (event.type === "keydown") {
        app.ctx.keyPress |= KEYS[key];
      }
    },
    {
      events: ["keydown", "keyup"]
    }
  );
};
